package com.cuckoo.cuckoo_app2.utils;

import android.content.DialogInterface;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.Context;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.Toast;

import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.cuckoo.cuckoo_app2.R;

/**
 * 弹窗工具类
 */
public class DialogUtils {

    private static AlertDialog alertDialog;
    private static Dialog loadingDialog;

    /**
     * 等单线程
     */
    public static void sleep(long millis) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
    }

    /**
     * 显示Toast
     *
     * @param context
     * @param message
     */
    public static void showToast(Context context, String message) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 100); // 修改位置
        toast.show();
    }

    /**
     * 显示一个简单的消息对话框
     *
     * @param context 上下文
     * @param title   标题
     * @param message 消息内容
     */
    public static void showMessageDialog(Context context, String title, String message, DialogInterface.OnClickListener positiveListener) {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("OK", positiveListener);

        alertDialog = builder.create();
        alertDialog.show();
    }

    /**
     * 隐藏当前显示的对话框
     */
    public static void hideCurrentDialog() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }

    /**
     * 显示带有确认和取消按钮的AlertDialog
     *
     * @param context
     * @param title
     * @param message
     * @param positiveListener
     * @param negativeListener
     */
    public static void showAlertDialog(Context context, String title, String message,
                                       DialogInterface.OnClickListener positiveListener,
                                       DialogInterface.OnClickListener negativeListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", positiveListener)
                .setNegativeButton("取消", negativeListener)
                .show();
    }

    /**
     * 仅显示带有确认按钮的AlertDialog
     *
     * @param context
     * @param title
     * @param message
     * @param positiveListener
     */
    public static void showConfirmDialog(Context context, String title, String message,
                                         DialogInterface.OnClickListener positiveListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", positiveListener)
                .setNegativeButton("取消", null) // 取消按钮不做处理
                .show();
    }

    /**
     * 显示一个自定义视图的对话框
     *
     * @param context 上下文
     * @param view    自定义视图
     */
    public static void showCustomViewDialog(Context context, int view) {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }

        LayoutInflater inflater = LayoutInflater.from(context);
        TextView textView = (TextView) inflater.inflate(view, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(textView)
                .setPositiveButton("Close", (dialog, which) -> dialog.dismiss());

        alertDialog = builder.create();
        alertDialog.show();
    }

    /**
     * 显示加载中的提示框
     *
     * @param context 上下文
     */
    public static void showLoadingDialog(Context context) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }

        loadingDialog = new Dialog(context);
        loadingDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        loadingDialog.setContentView(R.layout.dialog_loading);
        loadingDialog.setCancelable(false); // 设置不可取消

        // 调整Dialog的大小和位置
        Window window = loadingDialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = LinearLayout.LayoutParams.WRAP_CONTENT;
            lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            window.setDimAmount(0.7f);
        }

        loadingDialog.show();
    }

    /**
     * 关闭加载中的提示框
     */
    public static void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}